/**
  HomeMade by shoe[box]
 
  Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions are
  met:

  * Redistributions of source code must retain the above copyright notice, 
    this list of conditions and the following disclaimer.
  
  * Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the 
    documentation and/or other materials provided with the distribution.
  
  * Neither the name of shoe[box] nor the names of its 
    contributors may be used to endorse or promote products derived from 
    this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package org.shoebox.guacamole.view.containers {

	import org.shoebox.collections.HashMap;
	import org.shoebox.guacamole.core.RenderTask;
	import org.shoebox.guacamole.interfaces.IContainer;
	import org.shoebox.guacamole.interfaces.IRenderable;
	import org.shoebox.guacamole.view.GuaSprite;
	import org.shoebox.guacamole.view.abstracts.GuaProjectable;
	import org.shoebox.utils.logger.Logger;
	import flash.geom.Matrix3D;
	
	/**
	* org.shoebox.guacamole.view.MoleContainer
	* @author shoebox
	*/
	public class GuaContainer extends GuaProjectable implements IContainer , IRenderable {
		
		public var bHasChild			: Boolean = false;
		public var matrix			: Matrix3D;
		public var length			: uint = 0;
		
		protected var _iChildsCount		: int;
		protected var _hMap			: HashMap;
		protected var _vChilds			: Vector.<GuaProjectable>;
		
		// -------o constructor
		
			public function GuaContainer(  ) : void {
				super( );
				setSize( 1 , 1 ); //For projection only the contains is not rendered
				_iChildsCount = 0;
				
				matrix = new Matrix3D( );
				_hMap = new HashMap( true );
				_vChilds = new Vector.<GuaProjectable>( );
				removeAllChilds( );
			}

		// -------o public
			
			/**
			* addChild function
			* @public
			* @param 
			* @return
			*/
			public function addChild( a : GuaProjectable , b : Boolean = false ) : Boolean {
				
				if( _hMap.containsKey(a) )
					return false;
				
				_hMap.addItem( a , length ++ );
				
				a.scene = scene;
				a.parent = this;
				a.container = this;
				_vChilds.push( a );
				_iChildsCount = _vChilds.length;
				bHasChild = true;
				a.init( );
				
				return true;
			}
				
			/**
			* removeAllChilds function
			* @public
			* @param 
			* @return
			*/
			final public function removeAllChilds() : void {
				_hMap.removeAll( );
				_vChilds = new Vector.<GuaProjectable>( );
				_iChildsCount = 0;
			}
			
			/**
			* removeChildAt function
			* @public
			* @param 
			* @return
			*/
			final public function removeChildById( id : int ) : void {
				var a : GuaProjectable = _vChilds[ id ];
			}
			
			/**
			* removeChild function
			* @public
			* @param 
			* @return
			*/
			final public function removeChild( a : GuaProjectable ) : void {
				
				if( !_hMap.containsKey( a ) )
					return;
				
				var pos : int  = _vChilds.indexOf( a );
				
				if( pos == -1 )
					return;
				
				_hMap.removeKey( a );
				
				length--;
				a.scene = null;
				a.parent = null;
				
				_vChilds.splice( pos , 1 );
				_iChildsCount = _vChilds.length;
				//TODO:
				bHasChild = length > 0;
			}
			
			/**
			* removeChilds function
			* @public
			* @param 
			* @return
			*/
			final public function removeChilds( v : Vector.<GuaProjectable> ) : void {
				
				var i : int = 0 , l :int = v.length;
				for( i ; i < l ; i++ )
					removeChild( v[ i ] );
			}
			
			/**
			* addChilds function
			* @public
			* @param 
			* @return
			*/
			final public function addChilds( v : Vector.<GuaProjectable> ) : void {
				
				var i : int = 0 , l :int = v.length;
				var b : Boolean = false;
				for( i ; i < l ; i++ ){
					if( !b )
						b = addChild( v[ i ] );
					else
						addChild( v[ i ] );
				}
			
				if( b )
					zSort( );
			}
			
			/**
			* set depth function
			* @public
			* @param 
			* @return
			*/
			final override public function set depth( i : int ) : void {
				super.depth = i;
				zSort( );
			}
			
			
			
			/**
			* getChildDepth function
			* @public
			* @param 
			* @return
			*/
			final public function getChildDepth( c : GuaSprite ) : void {
				//TODO
			}
			
			/**
			* render function
			* @public
			* @param 
			* @return
			*/
			override public function render( oTask : RenderTask ) : void {
				
				if( bHasChild ){
					
					var i : int = 0;
					for( i ; i < _iChildsCount ; i++ ){
						_vChilds [ i ].render( oTask );
					}
				}
			}
			
			/**
			* getChilds function
			* @public
			* @param 
			* @return
			*/
			final public function getChilds( v : Vector.<GuaProjectable> ) : Vector.<GuaProjectable> {
				
				var i : int = 0;
				var c : GuaProjectable;
				for ( i ; i < _iChildsCount ; i++ ) {
					c = _vChilds [ i ];
					
					if( c is GuaContainer )
						v = ( c as GuaContainer ).getChilds( v );
				}
				
				v = v.concat( _vChilds );	
				return _vChilds;			
			}
			
			/**
			* zSort function
			* @public
			* @param 
			* @return
			*/
			final public function zSort() : void {
				_shellSort( _vChilds );
			}
			
			
			/**
			* Depth sorting func
			*
			* @param 	t1 : sorting item 1 	(IZSortable)
			* @param 	t2 : sorting item 2 	(IZSortable)
			* @return	sort result 		(int)
			*/
			final protected function _depthSorter( t1 : GuaProjectable , t2 : GuaProjectable ) : int {
				return ( t1.depth == t2.depth ? 0 : ( t1.depth < t2.depth ) ? -1 : 1 );
			}
			
		// -------o protected
			
			/*
			* The shell sort algorithm is quicker than the <code>Vector.sort()</code> function
			*
			* @param 	data : Data to be sorted (Vector.<IZSortable>)
			* @return	void
			*/
			protected function _shellSort( data : Vector.<GuaProjectable> ) : void {
				const n : int = data.length;
				var inc : int = int(n / 2 + .5);
				var temp : GuaProjectable;
				var j : int;
				while( inc ) {
					for(var i : int = inc;i < n;i++) {
						temp = data[i];
						j = i;
						while(j >= inc && data[int(j - inc)].getDepth() > temp.getDepth() ) {
							data[j] = data[int(j - inc)];
							j = int(j - inc);
						}
						data[j] = temp;
					}
					inc = int(inc / 2.2 + 0.5);
				}
			}
			
			
		// -------o misc

			public static function trc( ...args : Array ) : void {
				Logger.log(GuaContainer, args);
			}
	}
}
